<?php

class Roles_AjaxSearchController extends Zend_Controller_Action
{

    public function init()
    {
        $this->_helper->layout()->disableLayout();
        if (Zend_Auth::getInstance()->hasIdentity()) {
            $this->_user_info = Zend_Auth::getInstance()->getStorage()->read();
            $this->_acl = new Zwei_Admin_Acl($this->_user_info->user_name);
            $this->_page = "roles";
        } else {
            $this->_redirect('index/login');//[TODO] se espera un JSON hacer una salida ad-hok
        }
    }

    public function indexAction()
    {
        exit("bye :)");
    }

    public function aclRolesAction()
    {
        $this->view->status = "false";
        $this->view->message = "";
        $this->view->base_url = str_replace("/", "\\/", BASE_URL);
        $form = new Zwei_Utils_Form();//$_REQUEST
        
        $aclRoles = new AclRolesModel();
        $oSelect = $aclRoles->select();    

        if (!empty($form->role_name)) {
            $oSelect->where($aclRoles->getAdapter()->quoteInto("role_name = ?", $form->role_name));
        } 
        
        if (!empty($form->approved)) {
            $oSelect->where($aclRoles->getAdapter()->quoteInto("role_name = ?", $form->approved));
        } 
        
        Debug::writeBySettings($oSelect->__toString(), "query_log");
        
        try {
            $rows = $aclRoles->fetchAll($oSelect);
            $this->view->status = "true";
            $this->view->message = "ok";
        } catch (Zend_Db_Exception $e) {
            $this->view->status = "false";
            $this->view->message = $e->getMessage();
        }    
        
        if ($rows->count() < 0) {
            $this->view->message = "no hay registros";    
        }
        
        $this->view->value = $rows;
    }

    public function actionModuleRoleAction()
    {
        $this->view->status = "false";
        $this->view->message = "";
        $this->view->base_url = str_replace("/", "\\/", BASE_URL);
        $form = new Zwei_Utils_Form();//$_REQUEST
        include_once(APPLICATION_PATH . '/modules/roles/models/AclModulesPermissions.php');
        $aclPermissions = new Roles_AclModulesPermissions();
        //$oSelect = $aclPermissions->select();
        
        if (!empty($form->acl_roles_id)) {
            $this->view->related = isset($form->related) && $form->related == "1" ? true : false;
            $aclPermissions->setRelated($this->view->related);
            $aclPermissions->setRolesId($form->acl_roles_id);
            
            //if ($this->view->related === true) {
                      
                //$oSelect->where($aclPermissions->getAdapter()->quoteInto("$aclPermissions->_name_roles.id = ?", $form->acl_roles_id));
            //} else {
                //$oSelect->where($aclPermissions->getAdapter()->quoteInto("$aclPermissions->_name_roles.id != ?", $form->acl_roles_id));
            //}
                
        }
        
        if (!empty($form->accion)) {
            //$oSelect->where($aclPermissions->getAdapter()->quoteInto("$aclPermissions->name_permissions.title", $form->accion));
        }
         
        //$oSelect->where("$aclPermissions->_name_modules.title IS NOT NULL");//Permisos asignados a módulos borrados [TODO] borrar permisos al borrar módulos
        $oSelect = $aclPermissions->select();
        Debug::writeBySettings($oSelect->__toString(), "query_log");
        
        $rows = $aclPermissions->fetchAll($oSelect);
        
        if ($rows->count() > 0) {
            $this->view->status = "true";
            $this->view->message = "ok";
        } else {
            $this->view->message = "no hay registros";    
        }
        
        $this->view->value = $rows;
    }
}